package com.xmy.cultivate.util;

import com.xmy.cultivate.schedule.*;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class QuartzSchedulerManager{
    @Autowired
    private Scheduler scheduler;

    // 开始执行定时器
    public void startJob() throws SchedulerException {
        scheduler.clear();
        startMyJob(scheduler);//扣课时
        startMyJob2(scheduler);//上课提醒
        /***************************暂时不启用*********************************/
        //startMyJob3(scheduler);//成绩填写提醒
        //startMyJob4(scheduler);//课程顺延
        //startMyJob5(scheduler);//更新班型
        startMyJob8(scheduler);//更新招生数据，是否上课大于2次课信息
        //startMyJob9(scheduler);//取消上课提醒
        /***************************暂时不启用*********************************/
        //startMyJob6(scheduler);//补扣金额【调整到了扣课时方法里调整，因此这里注释掉】
        startMyJob7(scheduler);//续报数据更新
        startMyJob10(scheduler);//同步分班信息到其它系统
        startMyJob11(scheduler);//添加学生剩余课次记录
        startMyJob12(scheduler);//微信上课提醒推送【延迟消息推送,上课前2个小时，与上课前30分钟】
        startMyJob13(scheduler);//更新分班信息最后一条数据为1
        startMyJob14(scheduler);//添加分班信息到历史记录表
        startMyJob15(scheduler);//修改客户状态
        startMyJob16(scheduler);//第天1点执行，公共的接口
        startMyJob17(scheduler);//第天23点执行，积分汇总接口
        startMyJob18(scheduler);//第天06点执行，人事试岗数据推送
        scheduler.start();
    }

    // 获取Job信息
    public String getJobInfo(String name, String group) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    }

    // 修改某个任务的执行时间
    public boolean modifyJob(String name, String group, String time) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return date != null;
    }

    // 暂停所有任务
    public void pauseAllJob() throws SchedulerException {
        scheduler.pauseAll();
    }

    // 暂停某个任务
    public void pauseJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    }

    // 恢复所有任务
    public void resumeAllJob() throws SchedulerException {
        scheduler.resumeAll();
    }

    // 恢复某个任务
    public void resumeJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    }

    // 删除某个任务
    public void deleteJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    }

    // 启动任务1
    private void startMyJob(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myjob", "group1").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");//每5秒钟
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/30 * * * ? *");//每30分钟
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/10 * * * ? *");//每10分钟
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 38 17 30 06 ? 2023");//只执行一次
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("myjob", "group1")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    //上课提醒 启动任务2
    private void startMyJob2(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(WxRemindJob.class).withIdentity("job2", "group2").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 20 * * ? *");//每天20点
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job2", "group2")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 启动任务3
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob3(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(TranscriptJob.class).withIdentity("job3", "group3").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/60 * * * * ?");
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 15 * * ? *");//每天18点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 10 10 16 7 ? 2024");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job3", "group3")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 课程顺延
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob4(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(CourseAutoJob.class).withIdentity("job4", "group4").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/60 * * * * ?");
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 15 * * ? *");//每天18点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 10 15 19 1 ? 2024");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job4", "group4")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 更新班型
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob5(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(StudentClassTypeJob.class).withIdentity("job5", "group5").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/60 * * * * ?");
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 15 * * ? *");//每天18点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 49 11 6 9 ? 2023");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job5", "group5")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 补扣金额
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob6(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(PatchMoney.class).withIdentity("job6", "group6").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 30 22 * * ? *");//每天18点
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job6", "group6")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 续报数据更新
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob7(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(PresortJob.class).withIdentity("job7", "group7").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 3 * * ? *");//每天3点,13点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 33 14 18 1 ? 2024");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job7", "group7")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 更新招生数据，是否上课大于2次课信息
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob8(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(EnrollJob.class).withIdentity("job8", "group8").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 3 * * ? *");//每天3点,13点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 33 10 1 3 ? 2024");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job8", "group8")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 取消上课提醒
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob9(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(WxCalRemindJob.class).withIdentity("job9", "group9").build();
        // 基于表达式构建触发器
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 3 * * ? *");//每天3点,13点 备注： [秒] [分] [时] [日] [月] [周] [年]
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 10 20 27 4 ? 2024");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job9", "group9")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 同步分班信息到其它系统
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob10(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(SyncStudentGradeJob.class).withIdentity("job10", "job10").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job10", "job10")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 添加学生剩余课次记录
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob11(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(ResidueCourseJob.class).withIdentity("job11", "job11").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 0 * * ? *");//每天0点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ? *");//每5分钟
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 5 8 10 7 ? 2024");//只执行一次，2023年06月17时50分

        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job11", "job11")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 微信上课提醒推送【延迟消息推送,上课前2个小时，与上课前30分钟】
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob12(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(WxRemindMqJob.class).withIdentity("job12", "job12").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/30 * * * ? *");//每30分钟
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job12", "job12")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 更新分班信息最后一条数据为1
     * @param scheduler
     * @throws SchedulerException
     */
    private void startMyJob13(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(UpdateNotIsLastJob.class).withIdentity("job13", "job13").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ? *");//每5分钟
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job13", "job13")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startMyJob14(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(AddStudentGradeHistoryJob.class).withIdentity("job14", "job14").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 1 * * ? *");//每天1点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job14", "job14")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startMyJob15(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(UpdateCustomerStatusJob.class).withIdentity("job15", "job15").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 1 * * ? *");//每天1点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job15", "job15")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startMyJob16(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(CommonJob.class).withIdentity("job16", "job16").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 1 * * ? *");//每天1点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 5 * * ? *");//每天5点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job16", "job16")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startMyJob17(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(IntegralStatisticsJob.class).withIdentity("job17", "job17").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 23 * * ? *");//每天1点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 32 18 * * ? *");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job17", "job17")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void startMyJob18(Scheduler scheduler) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定其job只能是实现Job接口的实例
        JobDetail jobDetail = JobBuilder.newJob(PersonTryJob.class).withIdentity("job18", "job18").build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 6 * * ? *");//每天6点,0点 备注： [秒] [分] [时] [日] [月] [周] [年]
        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 45 16 * * ? *");//只执行一次，2023年06月17时50分
        // CronTrigger表达式触发器 继承于Trigger。TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("job18", "job18")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}
